package cn.itcast.Rainbow_self_mention.Activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.itcast.Rainbow_self_mention.R;
import cn.itcast.Rainbow_self_mention.Server.CountServer;

public class ResetPassword extends AppCompatActivity {
    public EditText  number_of_phone, number_of_identity, password, configer_password;
    Button reset_button;

    ImageView back;
    Intent server;
    MyServiceConn conn;
    CountServer.sender sender;
    static String sentresult;
    static Boolean jump ;
    static Boolean link ;
    static Boolean resist ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resetpassword);
        server = new Intent(this, CountServer.class);//创建意图对象
        conn = new MyServiceConn();//创建服务连接对象
        bindService(server, conn, BIND_AUTO_CREATE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        init();
        initbutton();
    }
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void init() {


        number_of_phone = findViewById(R.id.reset_number);
        number_of_identity = findViewById(R.id.reset_identity);
        password = findViewById(R.id.reset_passsword);
        configer_password = findViewById(R.id.reset_surepassword);
        reset_button = findViewById(R.id.reset);
        back = findViewById(R.id.reset_back);
        jump=false;
        link=false;
        resist=false;

    }
    class Swatch extends Thread {
        @Override
        public void run() {
            threadwait(1);


            if (jump) {
                Looper.prepare();
                Toast.makeText(ResetPassword.this, "修改成功", Toast.LENGTH_SHORT).show();
                Transaction t = new Transaction();
                t.start();
                Looper.loop();
                this.interrupt();
                try {
                    this.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            } else {

                if (resist) {
                    Looper.prepare();
                    Toast.makeText(ResetPassword.this, "修改失败", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                    this.interrupt();
                    try {
                        this.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    Looper.prepare();
                    Toast.makeText(ResetPassword.this, "用户不存在请注册", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                    this.interrupt();
                    try {
                        this.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }
    class Transaction extends Thread {
        @Override
        public void run() {
            super.run();
            Intent intent = new Intent(ResetPassword.this, Login.class);
            startActivity(intent);
            overridePendingTransition(0, 0);
        }
    }
    class MyServiceConn implements ServiceConnection { //用于实现连接服务
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            sender = (CountServer.sender) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }
    public boolean check() {
        int j = 0;
        if (number_of_identity.getText().toString().length() != 18) {
            number_of_identity.setText("");
            number_of_identity.setHint("身份证号码不正确");
            number_of_identity.setHintTextColor(getResources().getColor(R.color.red));
            j++;
        }

        if (!password.getText().toString().equals(configer_password.getText().toString())) {
            configer_password.setText("");
            configer_password.setHint("两次密码不一致");
            configer_password.setHintTextColor(getResources().getColor(R.color.red));
            j++;
        }
        if (j != 0) {
            return false;
        }
        return true;
    }
    public boolean checknull() {
        int i = 0;

        if (number_of_phone.getText().toString().equals("")) {
            number_of_phone.setHint("请输入电话号码");
            number_of_phone.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }
        if (number_of_phone.getText().toString().length() > 11) {
            number_of_phone.setHint("电话号码长度不正确");
            number_of_phone.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }
        if (number_of_identity.getText().toString().equals("")) {
            number_of_identity.setHint("请输入身份证");
            number_of_identity.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }
        if (password.getText().toString().equals("")) {
            password.setHint("请输入密码");
            password.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }
        if (password.getText().toString().length() > 30) {
            password.setHint("密码长度不能大于30");
            password.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }
        if (configer_password.getText().toString().equals("")) {
            configer_password.setHint("请再次输入密码");
            configer_password.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }
        if (configer_password.getText().toString().length() > 30) {
            configer_password.setHint("密码长度不能大于30");
            configer_password.setHintTextColor(getResources().getColor(R.color.red));
            i++;
        }

        if (i != 0) {
            return true;
        }
        return false;
    }

    public void threadwait(int time) {
        try {
            Thread.sleep(time * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void initbutton() {
        reset_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (check() && !checknull()) {
                    sender.Reset(
                            number_of_phone.getText().toString(),
                            number_of_identity.getText().toString(),
                            password.getText().toString());
                    Swatch sw = new Swatch();
                    sw.start();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public void finish(){
        super.finish();
        overridePendingTransition(0, 0);
    }
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            sentresult = bundle.getString("sentresult");
            if (sentresult.equals("网络连接错误")) {
                link = false;
            }
            if (sentresult.equals("网络连接成功")) {
                link = true;
            }
            if (sentresult.equals("网络连接断开")) {
                link = false;
            }
            if (sentresult.equals("修改成功")) {
                jump = true;
                resist=true;
            }
            if (sentresult.equals("修改失败")) {
                jump = false;

            }
            if (sentresult.equals("用户不存在")) {
                jump = false;
                resist=false;
            }

        }

    };


}
